<template>
<el-form style="width:60%;" :model="RePwd" :rules="rules2" ref="RePwd"  class="demo-ruleForm">
  <el-form-item prop="userPwd">
    <el-input type="password" v-model="RePwd.userPwd" size="large" placeholder="旧密码" ></el-input>
  </el-form-item>
  <el-form-item prop="pass">
    <el-input type="password" v-model="RePwd.pass" size="large" auto-complete="off"  placeholder="新密码"></el-input>
  </el-form-item>
  <el-form-item prop="newPwd">
    <el-input type="password" v-model="RePwd.newPwd" size="large" auto-complete="off"  placeholder="重复密码"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button style="width:22%;" size="large" type="primary" @click="submitForm('RePwd')">提交</el-button>
    <el-button style="width:21%;" size="large" @click="resetForm('RePwd')">重置</el-button>
  </el-form-item>
</el-form>

</template>

<script>
  export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.RePwd.newPwd !== '') {
            this.$refs.RePwd.validateField('newPwd');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.RePwd.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        RePwd: {
          userId: '',
          pass: '',
          newPwd: '',
          userPwd: ''
        },
        rules2: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          newPwd: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          userPwd: [
            { required: true, message: 'Please input your password', trigger: 'blur' },
            { min: 6, max: 16, message: 'must be over 6 numbers', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        var _this = this
        var user = JSON.parse(sessionStorage.getItem('BIGDATA_PLATFORM'))
        _this.RePwd.userId = user.userId
        console.log(user)
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$ajax.post('/api/repswd',_this.RePwd).then(function(response){
                if(0==response.data.code){
                  setTimeout(function () {
                    _this.$message({message: response.data.data,type: 'success'});
                    _this.$refs['RePwd'].resetFields();
                  }, 500);
                }else if(1==response.data.code){
                  _this.$message({message: response.data.msg,type: 'error'});
                }
                }).catch(function(error){console.log(error);}); 
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
